﻿//创建数据库对象 (用法和EF Dappper一样通过new保证线程安全)
using FastCode.Models;
using SqlSugar;
using System.Reflection;
namespace FastCode
{
    public class DBInit
    {

        //public static 
        public static async void Init(ISqlSugarClient? db)
        {
            //var db = SqlSugarHelper.Db;

            //建库
            db.DbMaintenance.CreateDatabase();//达梦和Oracle不支持建库

            var baseModelAssembly = Assembly.GetAssembly(typeof(BaseModel));
            if (baseModelAssembly == null)
            {
                throw new ArgumentNullException(nameof(baseModelAssembly));
            }

            List<Type> derivedTypes = baseModelAssembly// 获取包含 BaseModel 的程序集
               .GetTypes() // 获取程序集中所有类型
               .Where(t => t.BaseType == (typeof(BaseModel)) && !t.IsAbstract) // 筛选出所有非抽象继承自 BaseModel 的类
               .ToList(); // 转换为列表

            foreach (var type in derivedTypes)
            {
                //建表（看文档迁移）
                db.CodeFirst.InitTables(type); //所有库都支持     
                Console.WriteLine($"建表:{type.Name} 成功");
            }

            #region 用户
            if (await db.Queryable<User>().AnyAsync(x => x.UserName == "admin") == false)
            {
                await db.Insertable(new User
                {
                    UserName = "admin",
                    PasswordHash = "0rI9TOQ/pr7p++dnFPPNEbNqDKJss/9x9rI6yxcbzYk=",
                    LastLoginDate = DateTime.Now,
                    Salt = "BjhnyY96GwJGaddVwoC7TA=="
                }).ExecuteCommandAsync();
            }


            #endregion

            #region 配置路由
            var sysRouterList = new List<RouteConfig>()
            {
                new RouteConfig
                {
                    Path="/home",
                    Label="首页",
                    Component="/src/views/Home.vue",
                    IsShow=true,
                    Order=0,
                },
                 new RouteConfig
                {
                    Path="/Login",
                    Label="登录",
                    IsLayout=false,
                    Component="/src/views/User/Login.vue",
                    Order=0,
                },
                   new RouteConfig
                {
                    Path="/ChangePwd",
                    Label="修改密码",
                    IsLayout=true,
                    Component="/src/views/User/ChangePwd.vue",
                    Order=0,
                },
                //new RouteConfig
                //{
                //    Path = "/system",
                //    Label = "系统管理",
                //    IsShow=true,
                //    Component = "/src/views/System.vue",
                //    Order=5,
                //},
                new RouteConfig
                {
                    Path = "/DBConfig",
                    Label = "数据库配置",
                    IsShow=true,
                    Component = "/src/views/DBConfig.vue",
                    Order=10,
                    ParentId = 4, // 系统管理的子菜单
                },
                new RouteConfig
                {
                    Path = "/DBTable",
                    Label = "数据表配置",
                    IsShow=true,
                    Component = "/src/views/DBTable.vue",
                    Order=15,
                    ParentId = 4, // 系统管理的子菜单
                },
                new RouteConfig
                {
                    Path = "/curdtemplate",
                    Label = "CURDTemplate",
                    IsShow=true,
                    Component = "/src/views/CURDTemplate.vue",
                    Order=20,
                    ParentId = 4, // 系统管理的子菜单
                },
                new RouteConfig
                {
                    Path = "/routeconfig",
                    Label = "菜单配置",
                    IsShow=true,
                    Component = "/src/views/routeconfig/RouteConfig.vue",
                    Order=25,
                    ParentId = 4, // 系统管理的子菜单
                },
                new RouteConfig
                {
                    Path = "/user",
                    Label = "用户管理",
                    IsShow=true,
                    Component = "/src/views/User.vue",
                    Order=30,
                },
                new RouteConfig
                {
                    Path = "/user/list",
                    Label = "用户列表",
                    IsShow=true,
                    Component = "/src/views/User/List.vue",
                    Order=35,
                    ParentId = 9, // 用户管理的子菜单
                },
                new RouteConfig
                {
                    Path = "/user/role",
                    Label = "角色管理",
                    IsShow=true,
                    Component = "/src/views/User/Role.vue",
                    Order=40,
                    ParentId = 9, // 用户管理的子菜单
                },
            };

            //查所有路由
            var allRoutes = await db.Queryable<RouteConfig>().Where(x => !x.IsDeleted).ToListAsync();
            // 使用Except方法找出list1中不在list2中的元素
            var result = sysRouterList.ExceptBy(allRoutes.Select(a => a.Path), x => x.Path).ToList();
            if (result.Any())
            {
                await db.Insertable(result).ExecuteCommandAsync();
            }
            #endregion
        }


    }


}
